package com.xkdx.guangguang.fragment.my.setup;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.xkdx.caipiao.R;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.fragment.user.UserSharePrefence1;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;

public class PointFragment extends BaseFragment {
	TextView tv;
	Button back;
	ListView pointList;
	LinearLayout point_ll, pb_ll;
	View view = null;

	public List<Map<String, String>> list;
	PointAction pointAction;
	PointModule pointModule;
	PointPresistence pointPresistence;
    UserSharePrefence1 usrSP;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
        usrSP = new UserSharePrefence1(getActivity(), IConstants.SP_USER_NEW);
	}

	@Override
	public void setRetainInstance(boolean retain) {
		// TODO Auto-generated method stub
		super.setRetainInstance(retain);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.point, container, false);
		findView();
		setOnClick();
		showLoading();

		return view;
	}

	private void setAdapter() {
		point_ll.setVisibility(View.VISIBLE);
		pb_ll.setVisibility(View.GONE);
		PointAdapter adapter = new PointAdapter(getActivity());
		pointList.setAdapter(adapter);
	}

	class PointAdapter extends BaseAdapter {
		LayoutInflater inflater;

		public PointAdapter(Context context) {
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder;
			if (convertView == null) {
				viewHolder = new ViewHolder();
				convertView = inflater.inflate(R.layout.point_item, null);
//				viewHolder.im = (ImageView) convertView.findViewById(R.id.point_im);
				viewHolder.introduce = (TextView) convertView.findViewById(R.id.point_introduce1);
				viewHolder.time = (TextView) convertView.findViewById(R.id.point_time1);
				viewHolder.point = (TextView) convertView.findViewById(R.id.point_introduce2);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			viewHolder.introduce.setText(list.get(position).get("Memo"));
			viewHolder.time.setText(list.get(position).get("CreateTime"));
			viewHolder.point.setText(list.get(position).get("Point"));

			return convertView;
		}

		class ViewHolder {
//			ImageView im;
			TextView introduce, time, point;

		}

	}

	@Override
	public void showLoading() {
		dataLoadDialog.show();
		pointPresistence = new PointPresistence(this);
		pointPresistence.setActitons(pointAction);
		pointPresistence.setModule(pointModule);
		pointPresistence.execute();
		dataLoadDialog.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				if (pointPresistence != null) {
					pointPresistence.cancel(true);
				}
				getFragmentManager().popBackStack();

			}
		});
		dataLoadDialog.show();

	}

	@Override
	public void showLoaded(HashMap<String, AbsModule> map) {
		dataLoadDialog.dismiss();
		if (map.containsKey("0")) {
			list = pointModule.point.getPointList();
            tv.setText(usrSP.getScore() + "积分");
            if (list!=null&&list.size() != 0) {
				setAdapter();
			}

		} else {
//			refresh(map);
		}

	}

	@Override
	public void showClick(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

	@Override
	public void findView() {

		tv = (TextView) view.findViewById(R.id.point_tv);
		point_ll = (LinearLayout) view.findViewById(R.id.point_ll);
		back = (Button) view.findViewById(R.id.point_back);
		pointList = (ListView) view.findViewById(R.id.point_list);
		pb_ll = (LinearLayout) view.findViewById(R.id.exchange_pb_ll);

		pointAction = new PointAction(getActivity());
		pointModule = new PointModule();

	}

	@Override
	public void setOnClick() {

		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getActivity().finish();
			}
		});

	}

}
